package view.backend.customer;

import exceptionManager.ExceptionManager;
import java.awt.Font;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.SQLException;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import model.ZipCode;
import view.generaldesign.DesignLayout;

/**
 *
 * @author Sonny Sandberg
 */
public class AdminCustomerAddress
{
    private JPanel adminAddressPanel;
    private JTextField
            tFieldStreet,
            tFieldZip,
            tFieldCity;
    private AdminCustomer adminCustomer;

    public AdminCustomerAddress(AdminCustomer view)
    {
        adminCustomer = view;
        
        adminAddressPanel = new JPanel(null);
        adminAddressPanel.setBounds(0, 0, 300, 135);
        adminAddressPanel.setBorder(BorderFactory.createTitledBorder(DesignLayout.T_FIELD_RED, "Adresse", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, DesignLayout.STANDARD));
        adminAddressPanel.setOpaque(false);
        
        JLabel lblStreet = new JLabel("Gade");
        lblStreet.setBounds(10, 25, 150, 18);
        lblStreet.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD));
        
        JLabel lblZip = new JLabel("Post");
        lblZip.setBounds(10, 75, 150, 18);
        lblZip.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD));
        
        JLabel lblCity = new JLabel("By");
        lblCity.setBounds(60, 75, 150, 18);
        lblCity.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD));
        
        tFieldStreet = new JTextField();
        tFieldStreet.setBounds(10, 45, 250, 25);
        tFieldStreet.setBorder(DesignLayout.T_FIELD_RED);
        tFieldStreet.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        tFieldStreet.setFont(DesignLayout.STANDARD);
        tFieldStreet.addKeyListener(new KeyAdapter() 
        {
            @Override
            public void keyReleased(KeyEvent e)
            {
                adminCustomer.getAddressModel().setStreet(tFieldStreet.getText());
                adminCustomer.updateNewCustomerPanel();
            }
        });
        
        tFieldZip = new JTextField();
        tFieldZip.setBounds(10, 95, 40, 25);
        tFieldZip.setBorder(DesignLayout.T_FIELD_RED);
        tFieldZip.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        tFieldZip.setFont(DesignLayout.STANDARD);
        tFieldZip.addKeyListener(new KeyAdapter() 
        {
            @Override
            public void keyReleased(KeyEvent e)
            {
                if (tFieldZip.getText().length() == 4)
                {
                    try
                    {
                        ZipCode newZipCode = model.handlers.AddressHandler.getCity(tFieldZip.getText());
                        if (newZipCode != null)
                        {
                            adminCustomer.getAddressModel().setZipCode(newZipCode);
                            tFieldCity.setText(adminCustomer.getAddressModel().getZipCode().getCity());
                            adminCustomer.updateNewCustomerPanel();
                        }
                        else
                        {
                            tFieldZip.setText("");
                            tFieldCity.setText("");
                            adminCustomer.updateNewCustomerPanel();
                        }
                    } 
                    catch (SQLException ex)
                    {
                        ExceptionManager.handleSQLException(ex);
                    } 
                    catch (ClassNotFoundException ex)
                    {
                        ExceptionManager.handleClassNotFoundException(ex);
                    }
                }
                else if (tFieldZip.getText().length() > 4)
                {
                    tFieldZip.setText(""+tFieldZip.getText().subSequence(0, 4));
                    adminCustomer.updateNewCustomerPanel();
                }
                else
                {
                    adminCustomer.getAddressModel().getZipCode().clearModel();
                    tFieldCity.setText("");
                    adminCustomer.updateNewCustomerPanel();
                }
            }
        });
        
        tFieldCity = new JTextField();
        tFieldCity.setBounds(60, 95, 200, 25);
        tFieldCity.setBorder(DesignLayout.T_FIELD_RED);
        tFieldCity.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        tFieldCity.setFont(DesignLayout.STANDARD);
        tFieldCity.setEditable(false);
        
        adminAddressPanel.add(lblStreet);
        adminAddressPanel.add(lblZip);
        adminAddressPanel.add(lblCity);
        adminAddressPanel.add(tFieldStreet);
        adminAddressPanel.add(tFieldZip);
        adminAddressPanel.add(tFieldCity);
        
        adminAddressPanel.setVisible(true);
    }
    
    public void setFieldsFromModel()
    {        
        tFieldStreet.setText(adminCustomer.getAddressModel().getStreet());
        tFieldZip.setText(adminCustomer.getAddressModel().getZipCode().getZipCode());
        tFieldCity.setText(adminCustomer.getAddressModel().getZipCode().getCity());
    }

    public JPanel getPanel()
    {
        return adminAddressPanel;
    }
}